package compro.admission.form.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import compro.admission.form.entity.User;
import compro.admission.form.service.UserService;

@Controller
public class LoginController {
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping("/")
	public String showLogin(Model model) {
		return "login";
	}

	@RequestMapping("/signout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "login";
		
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(User user, HttpSession session) {
		User userLogin = userService.getUserLogin(user);
		if (userLogin == null) {
			return "redirect:/";
		} else {
			session.setAttribute("user",userLogin);
			if (userLogin.getRole().equalsIgnoreCase("applicant")) {
				return "redirect:/showApplication";
			} else if (userLogin.getRole().equalsIgnoreCase("admission")) {
				return "redirect:/showAdmissionForm";
			} else {
				return "redirect:/";
			}
		}
	}

}
